// pages/other//chourecord/index.js
const Page = require('../../../utils/ald-stat.js').Page;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recordList: '',
    page: 1,
    state:true,
    type:1,
    haveMore:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that=this;
    that.setData({
      type:options.type
    })
    if(options.type==2){
      wx.setNavigationBarTitle({ title: '中奖记录' })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    const type=that.data.type;
    that.data.page=1;
    that.data.haveMore=true;
    var obj = {
      unionid: wx.getStorageSync('openid'),
      type:type,
      page:1
    }
    var sign = app.sort_ASCII(obj)
    wx.request({
      url: app.globalData.new_url + '/assistant/Assistant/get_lucky_record',
      data: {
        unionid: wx.getStorageSync('openid'),
        type: type,
        page: 1,
        sign:sign
      },
      method: 'POST',
      header: {
        'Cache-Control': 'max-age=60,public', //60秒
      },
      success: function (res) {
        console.log(res)
        if(res.data.code===200){
          that.setData({
            recordList: res.data.content.list
          })
        }else{
          that.setData({
            state:false,
          })
        }
      }
    })
  },
  // 上拉触底事件，请求记录数据
  onReachBottom: function () {
    const that = this
    const type=that.data.type;
    let page = that.data.page;
    if (that.data.haveMore) {
      // 请求下一页数据
      page++;
      that.data.page = page
      var obj = {
        unionid: wx.getStorageSync('openid'),
        type: type,
        page: page
      }
      var sign = app.sort_ASCII(obj)
      wx.request({
        url: app.globalData.new_url + '/assistant/Assistant/get_lucky_record',
        data: {
          unionid: wx.getStorageSync('openid'),
          type: type,
          page: page,
          sign:sign
        },
        method: 'POST',
        success: function (res) {
          if(res.data.code===200){
            that.data.recordList = that.data.recordList.concat(res.data.content.list);
            that.setData({
              recordList: that.data.recordList,
            })
            if (res.data.content.count > 10) {
              that.data.haveMore = true
            } else {
              that.data.haveMore = false
            }
          }
        }
      })
    }
  },
  godetail:function(e){
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/other/choudetail/index?id=' + id,
    })
  }
})